// pages/index/index.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [],
    rankList: [],
    recommendList: []
  },
  goRecommend() {
    wx.navigateTo({
      url: '/pages/recommendSong/recommendSong',
    })
  },
  login() {
    wx.login({
      success:async (data)=> {
        // console.log(111,res.code,222);
        this.setData({
          code:data.code
        })
        let res =await request('getOpenId',{code:data.code})
        console.log( res);
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {

    this.login();
    let {
      banners
    } = await request('banner', {
      type: 2
    })
    this.setData({
      bannerList: banners
    })

    let {
      result
    } = await request('personalized', {
      limit: 10
    })
    this.setData({
      recommendList: result
    })

    let index = 0
    let rankList = []
    while (index < 5) {
      let res = await request('top/list', {
        idx: index++
      })
      let playlist = {
        name: res.playlist.name,
        tracks: res.playlist.tracks.slice(0, 3)
      }
      rankList.push(playlist)
    }
    this.setData({
      rankList
    })
    // console.log(2222);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})